package cn.casc.tools.er;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author max
 * @version 1.0.1
 * @className ERFrame
 * @description
 * @copyright Copyright (c) 2023
 * @company 天纲梦工厂
 * @datetime 2024/7/18 14:13
 **/

public class ERFrame extends JFrame {

    static String jdbcUrl = "jdbc:mysql://192.168.5.132:3306/information_schema?useSSL=false&useUnicode=true" +
            "&characterEncoding=utf-8" +
            "&allowMultiQueries=true";
    static String username = "root";
    static String password = "root123456";


    Engine engine = new Engine(jdbcUrl, username, password);
    public ERFrame() {
        this.setTitle("ER图分析引擎");
        this.setLayout(new BorderLayout());
        this.setSize(600, 400);
        this.init();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    JTextField tfSearch;
    JCheckBox ckUndefined;
    JList<EntityNode> nodes;
    ColumnTable columnTable;
    JTextArea textArea;
    JTextField tfCmd;
    void init() {
        JPanel left = new JPanel();
        JPanel right = new JPanel();
        {
            left.setMinimumSize(new Dimension(260,28));
            left.setLayout(new BorderLayout());
            right.setLayout(new BorderLayout());
            JSplitPane pane = new JSplitPane();
            pane.setLeftComponent(left);
            pane.setRightComponent(right);
            pane.setAutoscrolls(true);
            this.add(pane, BorderLayout.CENTER);
        }

        {
            nodes = new JList<>(engine.idToEntity.values().toArray(new EntityNode[0]));
            left.add(new JScrollPane(nodes), BorderLayout.CENTER);
            nodes.addListSelectionListener((evt) -> {
                this.show(nodes.getSelectedValue());
            });
        }

        {
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
            left.add(panel, BorderLayout.NORTH);
            this.tfSearch = new JTextField();
            panel.add(tfSearch, BorderLayout.CENTER);
            this.ckUndefined = new JCheckBox("?");
            panel.add(ckUndefined, BorderLayout.WEST);

            this.tfSearch.addActionListener((l) -> {
                this.search();
            });
            this.ckUndefined.addActionListener((l) -> {
                this.search();
            });
        }

        {
            columnTable = new ColumnTable(new ArrayList<>());
            textArea = new JTextArea();

            JSplitPane pane = new JSplitPane();
            JScrollPane spCT = new JScrollPane(columnTable);
            spCT.setMinimumSize(new Dimension(300,300));
            pane.setTopComponent(spCT);
            pane.setBottomComponent(new JScrollPane(textArea));
            pane.setOrientation(JSplitPane.VERTICAL_SPLIT);
            pane.setAutoscrolls(true);

            tfCmd = new JTextField();
            right.add(pane, BorderLayout.CENTER);
            right.add(tfCmd, BorderLayout.SOUTH);

            tfCmd.addActionListener((evt) -> {
                String txt = engine.exec(evt.getActionCommand());
                textArea.setText(txt);
            });
        }
    }


    public void search() {
        boolean isUndefined = this.ckUndefined.isSelected();
        String table = this.tfSearch.getText();
        List<EntityNode> likes = this.engine.mostLike(table, isUndefined, 10);
        this.nodes.setListData(likes.toArray(new EntityNode[0]));
    }

    public void show(EntityNode en) {
        if(en == null) {
            return;
        }
        this.columnTable.data.clear();
        this.columnTable.data.addAll(en.columns);
        this.columnTable.updateUI();
    }
}
